//
//  FPProductListController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/3/13.
//

#import "FPProductListController.h"
#import "FPHomeProductCell.h"

@interface FPProductListController () <UITableViewDelegate , UITableViewDataSource , DZNEmptyDataSetSource , DZNEmptyDataSetDelegate>

@end

@implementation FPProductListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 删除控制器
    NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPMeProfileTwoController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPExcessiveController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    self.navigationController.viewControllers = marr;
}

- (void)initUI {
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    [self setTitle:@"全部产品" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120.f;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    [FPProductModel getProductsWithType:(FPProductPostTypeAll) success:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        
        if (code == 200) {
            
            [self.dataSource removeAllObjects];
            
            for (FPProductModel *model in list) {
                
                [self.dataSource addObject:model];
            }
       
            [self.tableView reloadData];
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPHomeProductCell *cell;
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPHomeProductCell.class) owner:nil options:nil].lastObject;
    }
    
    cell.applyBtn.indexPath = indexPath;
    
    FPProductModel *model = self.dataSource[indexPath.row];
    
    [cell analysisUIWithProductModel:model];
    
    return cell;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return IMG(@"bg_nodata");
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {

    return - 80.f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    NSString *text = @"当前借款通道拥挤中，请五分钟后再次尝试。";
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    [NSString stringToAttribute:attStr font:FONT(12.f) range:NSMakeRange(0, text.length)];
    [NSString stringToAttribute:attStr color:[UIColor hexColor:@"#6A6A6A"] range:NSMakeRange(0, text.length)];

    return attStr;
}


@end
